﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FYP14
{
    public partial class AddMedicine : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["ID"] != null)
                {
                    SetValues(Convert.ToInt32(Request.QueryString["ID"]));
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            Medicine oMedicine = new Medicine();
            //oMedicine.Medicine_Prescription_id = txtMedicinePrescription.Text;
            oMedicine.Medicine_Type = txtMedicineType.Text;
            oMedicine.Medicine_Name = txtMedicineName.Text;
            oMedicine.Generic_Name = txtGenericName.Text;
            oMedicine.Chemical_Name = txtChemicalName.Text;
            oMedicine.Potency = txtPotency.Text;
            oMedicine.Dosage_Form = txtDostageForm.Text;

            if (Request.QueryString["ID"] != null)
            {
                oMedicine.Medicine_id = Convert.ToInt32(Request.QueryString["ID"]);
                BAL.UpdateMedicine(oMedicine);
            }
            else
            {
                BAL.SaveMedicine(oMedicine);
            }

            Response.Redirect("ViewMedicines.aspx");
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("ViewMedicines.aspx");
        }

        private void SetValues(Int32 ID)
        {
            DataTable dt = BAL.GetMedicine(ID);
            if (dt.Rows.Count > 0)
            {
                //txtMedicinePrescription.Text = dt.Rows[0]["medicine_prescription_id"].ToString();
                txtMedicineType.Text = dt.Rows[0]["medicine_type"].ToString();
                txtMedicineName.Text = dt.Rows[0]["medicine_name"].ToString();
                txtGenericName.Text = dt.Rows[0]["generic_name"].ToString();
                txtChemicalName.Text = dt.Rows[0]["chemical_name"].ToString();
                txtPotency.Text = dt.Rows[0]["potency"].ToString();
                txtDostageForm.Text = dt.Rows[0]["dosage_form"].ToString();
            }
        }
    }
}